﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;

namespace FangChanCrm.Services.Realization
{
    public class t_XiaoShouXiangXiBiaoService : It_XiaoShouXiangXiBiaoService
    {
        public readonly MyContext _Context;
        public t_XiaoShouXiangXiBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        public t_XiaoShouXiangXiBiao Add(t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao)
        {
            _Context.t_XiaoShouXiangXiBiao.Add(t_XiaoShouXiangXiBiao);
            _Context.SaveChanges();
            return _Context.t_XiaoShouXiangXiBiao.FirstOrDefault(x => x.BeiZhu1 == t_XiaoShouXiangXiBiao.BeiZhu1 && x.BeiZhu2 == t_XiaoShouXiangXiBiao.BeiZhu2 && x.BeiZhu3 == t_XiaoShouXiangXiBiao.BeiZhu3 && x.BeiZhu4 == t_XiaoShouXiangXiBiao.BeiZhu4 && x.BeiZhu5 == t_XiaoShouXiangXiBiao.BeiZhu5 && x.BeiZhu6 == t_XiaoShouXiangXiBiao.BeiZhu6 && x.BeiZhu7 == t_XiaoShouXiangXiBiao.BeiZhu7 && x.BeiZhu8 == t_XiaoShouXiangXiBiao.BeiZhu8 && x.BeiZhu9 == t_XiaoShouXiangXiBiao.BeiZhu9 && x.BeiZhu10 == t_XiaoShouXiangXiBiao.BeiZhu10 && x.XiaoShouDongZuo == t_XiaoShouXiangXiBiao.XiaoShouDongZuo && x.CaoZuoShiJian == t_XiaoShouXiangXiBiao.CaoZuoShiJian && x.CaoZuoRen == t_XiaoShouXiangXiBiao.CaoZuoRen && x.XiaoShouBiaoId == t_XiaoShouXiangXiBiao.XiaoShouBiaoId && x.ZhuangTai == t_XiaoShouXiangXiBiao.ZhuangTai && x.QueRenDaoFangRen == t_XiaoShouXiangXiBiao.QueRenDaoFangRen && x.MiaoShu == t_XiaoShouXiangXiBiao.MiaoShu);
        }

        public t_XiaoShouXiangXiBiao GetById(int id)
        {
            return _Context.t_XiaoShouXiangXiBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_XiaoShouXiangXiBiao> GetList()
        {
            return _Context.t_XiaoShouXiangXiBiao.Include(x => x.t_XiaoShouBiao).Include(x => x.t_XiaoShouBiao.t_KeHuBiao).ToList();
        }



        public bool Delete(int id)
        {
            _Context.t_XiaoShouXiangXiBiao.Remove(_Context.t_XiaoShouXiangXiBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public List<t_XiaoShouXiangXiBiao> GetByKeHuId(int Id)
        {
            return _Context.t_XiaoShouXiangXiBiao.Include(x => x.t_RenYuanBiao_CaoZuoRen).Where(x => x.XiaoShouBiaoId == _Context.t_XiaoShouBiao.Where(x => x.KeHuId == Id).FirstOrDefault().Id).OrderBy(x => x.CaoZuoShiJian).ToList();
        }


        public List<t_XiaoShouXiangXiBiao> GetXiaoShouXiangXi(int id, int kehuid)
        {
            return _Context.t_XiaoShouXiangXiBiao.Include(x => x.t_XiaoShouBiao).Where(x => x.t_XiaoShouBiao.YeWuYuanId == id && x.t_XiaoShouBiao.KeHuId == kehuid).DistinctBy(x => x.CaoZuoShiJian).OrderByDescending(x => x.CaoZuoShiJian).ToList();
        }

        public List<t_XiaoShouXiangXiBiao> GetListWithRenYuanAndXiaoShou()
        {
            return _Context.t_XiaoShouXiangXiBiao.Include(x => x.t_XiaoShouBiao).Include(x => x.t_RenYuanBiao_CaoZuoRen).ToList();
        }
    }
}
